package jmind.dolphin.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jmind.base.util.DataUtil;
import jmind.base.util.GlobalConstants;
import jmind.base.util.JmindLoader;
import jmind.dolphin.frame.ServiceFactory;


import java.util.StringTokenizer;

/**
 * Created by xieweibo on 2016/11/28.
 */
public class DophinUtil {




    public static final ServiceFactory getServiceFactory() {
        return JmindLoader.getJmindLoader(ServiceFactory.class).getExtension("serviceFactory");
    }

    /**
     * 解析参数形式的String
     * @param param
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T>  T parseParam(String param,Class<T> clazz){
        if (DataUtil.isEmpty(param))
            return null;
        JSONObject jo=new JSONObject();
        StringTokenizer token = new StringTokenizer(param, GlobalConstants.DELIM);
        while (token.hasMoreTokens()) {
            String[] ss=token.nextToken().split("=");
            if (ss.length == 2) {
                jo.put(ss[0], ss[1]);
            }
        }
        // 直接转换，性能高于转成String 在转换
        return JSON.toJavaObject(jo,clazz);
    }
}
